//
//  SettingViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/14.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "SettingViewController.h"
#import "ChangePassWordViewController.h"
#import "LoginViewController.h"
#import "KF5SDK.h"
#import "HomeList.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *titleArr;

@end

@implementation SettingViewController

- (NSArray *)titleArr{
    
    if (!_titleArr) {
        
        _titleArr = @[@"修改密码",@"退出登录"];
    }
    
    return _titleArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavBarWithTitle:@"设置"];
    
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.sectionHeaderHeight = 0.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.5)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    }
    
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if(cell==nil){
        
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:@"cell"];
        
    }
    
    cell.textLabel.text = self.titleArr[indexPath.section];
    cell.textLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
    cell.textLabel.textColor = [UIColor colorWithRed:56/255.0 green:56/255.0 blue:56/255.0 alpha:1];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0:
            
            [self pushChangePassWord];
            break;
            
        case 1:
            
            [self loginOut];
            break;
        default:
            break;
    }
    
}

#pragma mark - 修改面膜
- (void)pushChangePassWord{
    
    ChangePassWordViewController *VC = [[ChangePassWordViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    
}

#pragma mark - 退出登录
- (void)loginOut{
    MJWeakSelf
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定退出当前账号？" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [HomeList sharedInstance].closeToast = NO;
        
        [weakSelf.view makeToastActivity:CSToastPositionCenter];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"id"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"pwd"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"realname"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"headImage"];
        [KFUserManager deleteUser];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [weakSelf.view hideToastActivity];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [weakSelf.navigationController pushViewController:loginVC animated:YES];
        });
        
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    
}
@end
